//
//  MineHadSentInfoViewController.m
//  WeCommunity
//
//  Created by mac on 2017/6/15.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "MineHadSentInfoViewController.h"
#import "MineDynamicCell.h"
#import "MineDynamicDetailViewController.h"
#import "MJRefresh.h"

@interface MineHadSentInfoViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView * tableView;

@property (nonatomic,strong) NSMutableArray * dataSource;
@property (nonatomic,strong) MJRefreshAutoGifFooter * footer;

@end

@implementation MineHadSentInfoViewController

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KScreen_Width, KScreen_Height - KNaviheight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = WHITE_COLOR;

        self.footer = [MJRefreshAutoGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadingMoreData)];
        [self.footer setTitle:@"" forState:MJRefreshStateIdle];
        [self.footer setTitle:@"" forState:MJRefreshStateRefreshing];
        _tableView.mj_footer = _footer;
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.dataSource = [NSMutableArray array];
    
    [self initWithUserInterface];
    
    [self loadingMoreData];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deleteDynamicContentNotification:) name:DELETE_CONTENT_NOTIKEY object:nil];
}

-(void)initWithUserInterface
{
    self.title = KNSLocal(@"mineSentContentTitle");
    
    [self.view addSubview:self.tableView];

}

#pragma mark - 加载数据
-(void)loadingMoreData
{
    NSDate * loadDate = nil;
    if(self.dataSource.count > 0){
        loadDate = ((DynamicModel *)self.dataSource.lastObject).createDate;
    }
    
    [[DynamicServer shared] loadSomeOneDynamicDataWithContactId:[UserInfoManger shared].contact.objectId
                                                      loadDate:loadDate
                                                     loadCount:20
                                                      complete:^(BOOL success, NSString *error, NSArray *dataArray) {
          if (success) {
              [self.dataSource addObjectsFromArray:dataArray];
              
              if (dataArray.count == 0) {
                  [self.footer setTitle:KHasLoadedAllTitle forState:MJRefreshStateIdle];
                  [self.footer setTitle:KHasLoadedAllTitle forState:MJRefreshStateRefreshing];
              }else{
                  [self.footer setTitle:KDragOrClickToDragTitle forState:MJRefreshStateIdle];
                  [self.footer setTitle:KLoaddingMoreTitle forState:MJRefreshStateRefreshing];
              }
          }else{
              [UITool showHudWithText:error withView:self.view];
          }
                                                          
          [self endRefreshAnimation];
    }];
}

-(void)endRefreshAnimation
{
    [self.tableView.mj_footer endRefreshing];
    [self.tableView reloadData];
}


#pragma mark - tableView delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DynamicModel * model = self.dataSource[indexPath.row];
    return model.mineContentHeight;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellID = @"mineDynamicCell";
    MineDynamicCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell =[[MineDynamicCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    [cell layoutViewWithModel:self.dataSource[indexPath.row]];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    MineDynamicDetailViewController * detailVC = [[MineDynamicDetailViewController alloc] init];
    detailVC.detailModel = self.dataSource[indexPath.row];
    [self.navigationController pushViewController:detailVC animated:YES];
}
#pragma mark - 删除动态通知
-(void)deleteDynamicContentNotification:(NSNotification *)noti
{
    NSString * objectId = noti.object;
    for (DynamicModel * model in self.dataSource) {
        if ([model.objectId isEqualToString:objectId]) {
            [self.dataSource removeObject:model];
            [self.tableView reloadData];
            break;
        }
    }
}




@end
